//
//  MessageViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/11.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "MessageViewController.h"
#import "ZLContactTopView.h"
#import "ConversationListController.h"
//#import "SessionViewController.h"
#import "NewsViewController.h"
#import "NotificationViewController.h"
#import "BookViewController.h"
#import "SelectMemberViewController.h"
#import "CompanyViewController.h"

@interface MessageViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)ZLContactTopView *topView;
@property (nonatomic, assign)NSInteger index;
//@property (nonatomic,strong)SessionViewController *sessionVC;
@property (nonatomic,strong)ConversationListController *converSationList;
@property (nonatomic,strong)NewsViewController *newsVC;
@property (nonatomic,strong)NotificationViewController *notifiVC;
@property (nonatomic,strong)BookViewController *bookVC;
@property (nonatomic,weak) UIScrollView *scroll;
@property (nonatomic,strong)UIButton *rightButton;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _initSubView];
    [self _addSubCtrls];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self viewWillAppearLoadData];
    
}
- (void)viewWillAppearLoadData{
    [self.scroll setContentOffset:CGPointMake(self.index*kSCREEN_WIDTH, 0) animated:YES];
    switch (self.index) {
        case 0:
            
//            [self.sessionVC customViewWillAppear];
            [self.converSationList customViewWillAppear];
            
            
            
            break;
        case 1:
            [self.newsVC customViewWillAppear];
            
            break;
        case 2:
            [self.notifiVC customViewWillAppear];
            
            break;
        case 3:
            [self.bookVC customViewWillAppear];
        
            break;
            
        default:
            break;
    }
}

- (void)_initSubView {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightButton];
    
    _topView = [[ZLContactTopView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 50) withTitleArray:@[@"会   话",
                                                                                                                        @"通   知",@"通讯录"]];
    _topView.changLineColor = [UIColor colorWithHexString:@"454545"];
    __block typeof (self)weakSelf = self;
    _topView.topBlock = ^(NSInteger index) {
        weakSelf.index = index;
        [weakSelf.scroll setContentOffset:CGPointMake(index*kSCREEN_WIDTH, 0) animated:YES];
        [weakSelf viewWillAppearLoadData];

        //创建右侧按钮
        [weakSelf createRightButtonIndex:index];

    };
    [self.view addSubview:_topView];
}

- (void)createRightButtonIndex:(NSInteger )index {
    
    if (index == 0) {
        //群组
        self.rightButton.hidden = NO;
        
    }else {
        self.rightButton.hidden = YES;
    }
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, 40, 40);

        [_rightButton setImage:[UIImage imageNamed:@"qunzu_btn"] forState:UIControlStateNormal];
        [_rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}
- (void)rightButtonAction {
    
//    SelectMemberViewController *selectVC = [[SelectMemberViewController alloc] init];
//    selectVC.isCreat = YES;
//    [self.navigationController pushViewController:selectVC animated:YES];
    NSArray *arr = [DataCenter shareInstence].companyList;
    
    if (arr.count == 1) {
        SelectMemberViewController *selectVC = [[SelectMemberViewController alloc] init];
            selectVC.isCreat = YES;
        CompanyModel *model = [arr lastObject];
        selectVC.companyId = model.companyId;
        [self.navigationController pushViewController:selectVC animated:YES];
    }else{
        CompanyViewController *vc = [[CompanyViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)_addSubCtrls {
    
    NSMutableArray *ctrlsArray = [[NSMutableArray alloc]init];
    
//    SessionViewController *sessionCtrl = [[SessionViewController alloc]init];
//    self.sessionVC = sessionCtrl;
    ConversationListController *converList = [[ConversationListController alloc] init];
    self.converSationList = converList;
    [ctrlsArray addObject:converList];
    
    
    NotificationViewController *nitifyCtrl = [[NotificationViewController alloc]init];
    self.notifiVC = nitifyCtrl;
    [ctrlsArray addObject:nitifyCtrl];

    BookViewController *bookCtrl = [[BookViewController alloc]init];
    self.bookVC = bookCtrl;
    [ctrlsArray addObject:bookCtrl];
    
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_topView.frame), kSCREEN_WIDTH, kSCREEN_HEIGHT-CGRectGetMaxY(_topView.frame)-49-64)];
    scroll.delegate = self;
    scroll.pagingEnabled = YES;
    [self.view addSubview:scroll];
    self.scroll = scroll;
    for (int i = 0;i<ctrlsArray.count ;i++) {
        UIViewController *vc = ctrlsArray[i];
        UIView *view = vc.view;
        view.frame = CGRectMake(i*kSCREEN_WIDTH, 0, kSCREEN_WIDTH, scroll.height);
        [self addChildViewController:vc];
        [scroll addSubview:view];
        
    }
    
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/kSCREEN_WIDTH;
    _topView.selectIndex = index;
    self.index = index;
    [self viewWillAppearLoadData];
}

@end
